// /common/http.api.js

// 如果没有通过拦截器配置域名的话，可以在这里写上完整的URL(加上域名部分)

const baseUrl = '/api/private/v1'
const public_url = '/api/public/v1'

// 登录和注册
const login_url = public_url + '/login';
const register_url = `${login_url}/register`;
// 短信登录
const msg_url = public_url + '/msg'
const msg_login_url = msg_url + '/login'

// 首页
const banner_url = baseUrl + '/banner'
const notice_url = baseUrl + '/notice/get'
const tabs_url = baseUrl + '/tabs/get'
const product_url = baseUrl + '/product/get'

// 用户管理
const user_url = baseUrl + '/users'
const get_user_url = user_url + '/get'
const get_user_by_id_url = user_url + '/getById'
const edit_user_url = user_url + '/edit'

// 店铺管理
const shop_url = baseUrl + '/shops'
const get_shop_url = shop_url + '/get'
const get_shop_hot_url = get_shop_url + 'Hot'
const get_shop_by_id_url = shop_url + '/getById'

// 商品管理
const good_url = baseUrl + '/goods'
const get_goods_by_shop_id = good_url + '/getByShopId'

// 分类管理
const cate_url = baseUrl + '/categories'
const get_cate_by_id = cate_url + '/getById'

// 图片上传
const upload_url = baseUrl + '/upload'

// 此处第二个参数vm，就是我们在页面使用的this，你可以通过vm获取vuex等操作，更多内容详见uView对拦截器的介绍部分：
// https://uviewui.com/js/http.html#%E4%BD%95%E8%B0%93%E8%AF%B7%E6%B1%82%E6%8B%A6%E6%88%AA%EF%BC%9F
const install = (Vue, vm) => {
	// 此处使用了传入的params参数，一切自定义即可
	// 登录和注册
	const login = (data = {}) => vm.$u.post(login_url, data);
	const register = (data = {}) => vm.$u.post(register_url, data);
	// 短信登录
	const msg = (params = {}) => vm.$u.get(msg_url, params);
	const msgLogin = (data = {}) => vm.$u.post(msg_login_url, data);

	// 用户管理
	const getUser = (params = {}) => vm.$u.get(get_user_url, params);
	const getUserById = (params = {}) => vm.$u.get(get_user_by_id_url, params);
	const editUser = (data = {}) => vm.$u.post(edit_user_url, data);

	// 店铺管理
	const getShop = (params = {}) => vm.$u.get(get_shop_url, params);
	const getShopById = (params = {}) => vm.$u.get(get_shop_by_id_url, params);
	const getShopHot = () => vm.$u.get(get_shop_hot_url);

	// 商品管理
	const getGoodsByShopId = (params = {}) => vm.$u.get(get_goods_by_shop_id, params);

	// 分类管理
	const getCateById = (params = {}) => vm.$u.get(get_cate_by_id, params);

	// 图片上传
	const upload = (data = {}) => vm.$u.post(upload_url, data);

	// 首页
	const banner = () => vm.$u.get(banner_url);
	const notice = () => vm.$u.get(notice_url)
	const tabs = () => vm.$u.get(tabs_url)
	const product = () => vm.$u.get(product_url)

	// 将各个定义的接口名称，统一放进对象挂载到vm.$u.api(因为vm就是this，也即this.$u.api)下
	vm.$u.api = {
		login,
		register,
		getUser,
		getUserById,
		editUser,
		getShop,
		getShopById,
		getShopHot,
		upload,
		banner,
		notice,
		tabs,
		product,
		msg,
		msgLogin,
		getGoodsByShopId,
		getCateById
	};
	/* 
		使用方法
		this.$u.api.login( { id:3 } ).then( res => console.log( res ) )
	 */
}

export default {
	install
}
